package com.fenghongzhang;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.fenghongzhang.day008.R;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;


/**
 * 1,清单申请权限
 * 2,页面中动态申请权限
 * 3,接收申请权限的结果
 * 4,写
 * 5,读
 */
public class SDActivity extends AppCompatActivity {
    private Button write;

    private static final String TAG = "SDActivity";
    private Button read;


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        //如果请求是100 并且用户点击的是同意
        if(requestCode == 100 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
            Toast.makeText(this, "同意", Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(this, "不同意", Toast.LENGTH_SHORT).show();
            finish();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_s_d);





        requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE},100);

        //动态权限
        write = (Button) findViewById(R.id.write);
        read = (Button) findViewById(R.id.read);

        //读
        read.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                File externalStorageDirectory = Environment.getExternalStorageDirectory();
                FileInputStream fileInputStream = null;
                try {
                    fileInputStream = new FileInputStream(new File(externalStorageDirectory, "one.txt"));

                    //bit 位
                    //1 byte  8
                    //2次方
                    byte[] bytes = new byte[1024];
                    int len = 0;

                    StringBuilder stringBuilder = new StringBuilder();
                    while((len = fileInputStream.read(bytes)) != -1){
                        String s = new String(bytes, 0, len);
                        stringBuilder.append(s);
                        Toast.makeText(SDActivity.this, s, Toast.LENGTH_SHORT).show();
                    }
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        });

        write.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //写内容
                /**
                 * 下载
                 *
                 *
                 * 写
                 */

                String externalStorageState = Environment.getExternalStorageState();
                Log.i(TAG, "onClick: "+externalStorageState);

                if(externalStorageState.equals("mounted")){
                    File externalStorageDirectory = Environment.getExternalStorageDirectory();
                    Log.i(TAG, "onClick: "+externalStorageDirectory);

                    try {
                        FileOutputStream fileOutputStream = new FileOutputStream(new File(externalStorageDirectory, "one.txt"));
                        fileOutputStream.write("hehe".getBytes());
                        fileOutputStream.flush();
                        fileOutputStream.close();

                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }


            }
        });

    }
}
